package main

import (
	"flag"
	"fmt"
	"log"
	"net/http"
	"team-evaluate/global"
	"team-evaluate/internal/model"
	"team-evaluate/internal/routers"
	"team-evaluate/pkg/app"
	"team-evaluate/pkg/logger"
	"team-evaluate/pkg/setting"
	"time"

	"go.uber.org/zap/zapcore"

	"gopkg.in/natefinch/lumberjack.v2"
)

var (
	isVersion    bool
	buildTime    string
	buildVersion string
	gitCommitID  string
)

// @title 团队互评系统
// @version 1.0
// @description 企业级的团队互评系统
// @termsOfService https://team.wkartist.com

// @host 127.0.0.1:9527
func main() {
	if isVersion {
		fmt.Printf("build_time: %s\n", buildTime)
		fmt.Printf("build_version: %s\n", buildVersion)
		fmt.Printf("git_commit_id: %s\n", gitCommitID)
		return
	}

	router := routers.NewRouter()
	s := &http.Server{
		Addr:           ":" + global.ServerSetting.HttpPort,
		Handler:        router,
		ReadTimeout:    global.ServerSetting.ReadTimeout,
		WriteTimeout:   global.ServerSetting.WriteTimeout,
		MaxHeaderBytes: 1 << 20,
	}
	//global.Logger.Infof("%s: go-programming-tour-book/%s", "eddycjy", "blog-service")
	err := s.ListenAndServe()
	if err != nil {
		log.Fatalf("server start failed, err: %v", err)
	}
}

func init() {
	err := setupFlag()
	if err != nil {
		log.Fatalf("init.setupFlag err: %v", err)
	}
	err = setupSetting()
	if err != nil {
		log.Fatalf("init.setupSetting err: %v", err)
	}
	log.Printf("global.ServerSetting: %v", global.ServerSetting)
	log.Printf("global.DatabaseSetting: %v", global.DatabaseSetting)
	err = setupLogger()
	if err != nil {
		log.Fatalf("init.setupLogger err: %v", err)
	}
	err = setupDBEngine()
	if err != nil {
		log.Fatalf("init.setupDBEngine err: %v", err)
	}
	err = setupRedis()
	if err != nil {
		log.Fatalf("init.Redis err: %v", err)
	}
}

func setupFlag() error {
	flag.BoolVar(&isVersion, "version", false, "编译信息")
	flag.Parse()
	return nil
}

func setupSetting() error {
	s, err := setting.NewSetting()
	if err != nil {
		return err
	}
	err = s.ReadSection("Server", &global.ServerSetting)
	if err != nil {
		return err
	}
	err = s.ReadSection("App", &global.AppSetting)
	if err != nil {
		return err
	}
	err = s.ReadSection("Database", &global.DatabaseSetting)
	if err != nil {
		return err
	}
	global.ServerSetting.ReadTimeout *= time.Second
	global.ServerSetting.WriteTimeout *= time.Second
	err = s.ReadSection("Redis", &global.RedisSetting)
	if err != nil {
		return err
	}
	err = s.ReadSection("JWT", &global.JWTSetting)
	if err != nil {
		return err
	}
	global.JWTSetting.Expire *= time.Second
	return nil
}

func setupLogger() error {
	lumberJackLogger := &lumberjack.Logger{
		Filename:   global.AppSetting.LogSavePath + "/" + global.AppSetting.LogFileName + global.AppSetting.LogFileExt,
		MaxSize:    global.AppSetting.LogMaxSize,
		MaxBackups: global.AppSetting.LogMaxBackups,
		MaxAge:     global.AppSetting.LogMaxAges,
		Compress:   false,
		LocalTime:  true,
	}
	logger.InitLogger(zapcore.AddSync(lumberJackLogger))
	defer global.Logger.Sync()
	return nil
}

func setupDBEngine() error {
	var err error
	global.DBEngine, err = model.NewDBEngine(global.DatabaseSetting)
	if err != nil {
		return err
	}
	return nil
}

func setupRedis() error {
	var err error
	global.RDb, err = app.NewRedisDb(global.RedisSetting)
	if err != nil {
		return err
	}
	return nil
}
